﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RealEstate.Models;
using RealEstate.Common;
using Telerik.Web.UI;

namespace RealEstate
{
    public partial class NewTran : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Roles.IsUserInRole("admin"))
            {
                Response.Redirect("ByREStatus.aspx");
            }

            if (!IsPostBack)
            {
                Utilities.LoadClientForDropDownlist(ddlClient);
                Utilities.LoadClientForDropDownlist(ddlCoClient);
                Utilities.LoadPropertyForDropDownlist(ddlSubject);
                Utilities.LoadPropertyForDropDownlist(ddlMailing);
                Utilities.LoadClientTypeForDropDownlist(ddlClientType);
                Utilities.LoadSourceOfBusinessForDropDownlist(ddlSourceOfBusiness);
                Utilities.LoadUrgencyForDropDownlist(ddlUrgency);
                Utilities.LoadStateForDropDownlist(ddlState1);
                Utilities.LoadStateForDropDownlist(ddlState2);
                Utilities.LoadCityForDropDownlist(ddlCity1);
                Utilities.LoadCityForDropDownlist(ddlCity2);
                Utilities.LoadPropertyTypeForDropDownlist(ddlSubjectPropertyType);
                Utilities.LoadStatusForDropDownlist(ddlCaseStatus);
                txtClientHomePhone.Attributes.Add("onkeypress", string.Concat("ClientHomePhoneOnKeyPress('", txtClientHomePhone.ClientID, "_text'", ",'", txtCoClientHomePhone.ClientID, "_text'", ")"));
            }
        }

        protected void imgEdit_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ClientEntities clientEntity = new ClientEntities();
                PropertyEntities propertyEntity = new PropertyEntities();
                TransactionStatusEntities statusEntity = new TransactionStatusEntities();

                TransactionEntities entity = new TransactionEntities();
                TRANSACTION item = new TRANSACTION();
                item.Form = true;

                //Case Information
                if (rdpDateOpen.SelectedDate != null)
                    item.DateOpen = rdpDateOpen.SelectedDate;
                else
                    item.DateOpen = DateTime.Now;
                item.CaseIDNumber = txtCaseIDNumber.Text;
                item.Archive = chkArchive.Checked;

                //ClientInformation
                if (ddlClient.Text != string.Empty)
                {
                    Client client = clientEntity.GetById(int.Parse(ddlClient.SelectedItem.Value));
                    client.LastName = txtClientLastName.Text;
                    client.FirstName = txtClientFirstName.Text;
                    client.SSN = txtClientSSN.Text;
                    client.WorkPhNumber = txtClientWorkPhone.Text;
                    client.FaxNumber = txtClientFaxNumber.Text;
                    client.HomePhNumber = txtClientHomePhone.Text;
                    client.CellNumber = txtClientCellPhone.Text;
                    client.Email = txtClientEmail.Text;
                    if (rdpClientBirthDate.SelectedDate != null)
                        client.BirthDate = rdpClientBirthDate.SelectedDate;

                    clientEntity.Update(client);

                    item.ClientId = client.ClientId;
                }
                else if (!string.IsNullOrEmpty(txtClientLastName.Text) || !string.IsNullOrEmpty(txtClientFirstName.Text))
                {
                    Client client = new Client();
                    client.LastName = txtClientLastName.Text;
                    client.FirstName = txtClientFirstName.Text;
                    client.SSN = txtClientSSN.Text;
                    client.WorkPhNumber = txtClientWorkPhone.Text;
                    client.FaxNumber = txtClientFaxNumber.Text;
                    client.HomePhNumber = txtClientHomePhone.Text;
                    client.CellNumber = txtClientCellPhone.Text;
                    client.Email = txtClientEmail.Text;
                    if (rdpClientBirthDate.SelectedDate != null)
                        client.BirthDate = rdpClientBirthDate.SelectedDate;

                    clientEntity.Add(client);
                    if (client.ClientId > 0)
                        item.ClientId = client.ClientId;
                }

                if (ddlCoClient.Text != string.Empty)
                {
                    Client coClient = clientEntity.GetById(int.Parse(ddlCoClient.SelectedItem.Value));
                    coClient.LastName = txtCoClientLastName.Text;
                    coClient.FirstName = txtCoClientFirstName.Text;
                    coClient.SSN = txtCoClientSSN.Text;
                    coClient.WorkPhNumber = txtCoClientWorkPhone.Text;
                    coClient.FaxNumber = txtCoClientFaxNumber.Text;
                    coClient.HomePhNumber = txtCoClientHomePhone.Text;
                    coClient.CellNumber = txtCoClientCellPhone.Text;
                    coClient.Email = txtCoClientEmail.Text;
                    if (rdpCoClientBirthDate.SelectedDate != null)
                        coClient.BirthDate = rdpCoClientBirthDate.SelectedDate;

                    clientEntity.Update(coClient);
                    item.CoClientId = coClient.ClientId;
                }
                else if (!string.IsNullOrEmpty(txtCoClientLastName.Text) || !string.IsNullOrEmpty(txtCoClientFirstName.Text))
                {
                    Client coClient = new Client();
                    coClient.LastName = txtCoClientLastName.Text;
                    coClient.FirstName = txtCoClientFirstName.Text;
                    coClient.SSN = txtCoClientSSN.Text;
                    coClient.WorkPhNumber = txtCoClientWorkPhone.Text;
                    coClient.FaxNumber = txtCoClientFaxNumber.Text;
                    coClient.HomePhNumber = txtCoClientHomePhone.Text;
                    coClient.CellNumber = txtCoClientCellPhone.Text;
                    coClient.Email = txtCoClientEmail.Text;
                    if (rdpCoClientBirthDate.SelectedDate != null)
                        coClient.BirthDate = rdpCoClientBirthDate.SelectedDate;

                    //Add CoClient
                    clientEntity.Add(coClient);
                    if (coClient.ClientId > 0)
                        item.CoClientId = coClient.ClientId;
                }

                //Transaction Information                
                if (ddlClientType.Text != string.Empty)
                    item.ClientTypeId = int.Parse(ddlClientType.SelectedItem.Value);

                if (ddlSourceOfBusiness.Text != string.Empty)
                    item.SourceOfBusinessId = int.Parse(ddlSourceOfBusiness.SelectedItem.Value);

                if (ddlUrgency.Text != string.Empty)
                    item.UrgencyId = int.Parse(ddlUrgency.SelectedItem.Value);

                item.ReferedBy = txtReferedBy.Text;

                //Property Information        
                if (ddlSubject.Text != string.Empty)
                {
                    Property subject = propertyEntity.GetById(int.Parse(ddlSubject.SelectedItem.Value));
                    if (!string.IsNullOrEmpty(ddlSubjectPropertyType.Text))
                        subject.PropertyTypeId = int.Parse(ddlSubjectPropertyType.SelectedItem.Value);
                    subject.StreetNumer = txtSubjectStreetNumber.Text;
                    subject.StreetName = txtSubjectStreetName.Text;
                    if (!string.IsNullOrEmpty(ddlState1.Text))
                        subject.StateId = int.Parse(ddlState1.SelectedItem.Value);
                    if (!string.IsNullOrEmpty(ddlCity1.Text))
                        subject.CityId = int.Parse(ddlCity1.SelectedItem.Value);
                    subject.ZipCode = txtZipCode.Text;
                    //Update SubjectAddress
                    propertyEntity.Update(subject);
                    item.SubjectAddressId = subject.PropertyId;
                }
                else if (!string.IsNullOrEmpty(txtSubjectStreetNumber.Text) || !string.IsNullOrEmpty(txtSubjectStreetName.Text))
                {
                    Property subject = new Property();
                    if (!string.IsNullOrEmpty(ddlSubjectPropertyType.Text))
                        subject.PropertyTypeId = int.Parse(ddlSubjectPropertyType.SelectedItem.Value);
                    subject.StreetNumer = txtSubjectStreetNumber.Text;
                    subject.StreetName = txtSubjectStreetName.Text;
                    if (!string.IsNullOrEmpty(ddlState1.Text))
                        subject.StateId = int.Parse(ddlState1.SelectedItem.Value);
                    if (!string.IsNullOrEmpty(ddlCity1.Text))
                        subject.CityId = int.Parse(ddlCity1.SelectedItem.Value);
                    subject.ZipCode = txtZipCode.Text;
                    //Add SubjectAddress
                    propertyEntity.Add(subject);
                    if (subject.PropertyId > 0)
                        item.SubjectAddressId = subject.PropertyId;
                }

                //Add mailing
                if (ddlMailing.Text != string.Empty)
                {
                    Property mail = propertyEntity.GetById(int.Parse(ddlMailing.SelectedItem.Value));
                    mail.StreetNumer = txtMailStreetNumber.Text;
                    mail.StreetName = txtMailStreetName.Text;
                    if (!string.IsNullOrEmpty(ddlState2.Text))
                        mail.StateId = int.Parse(ddlState2.SelectedItem.Value);
                    if (!string.IsNullOrEmpty(ddlCity2.Text))
                        mail.CityId = int.Parse(ddlCity2.SelectedItem.Value);
                    mail.ZipCode = txtZipCode_New.Text;
                    //Update MaillingAddress
                    propertyEntity.Update(mail);
                    item.MailingAddressId = mail.PropertyId;
                }
                else if (!string.IsNullOrEmpty(txtMailStreetNumber.Text) || !string.IsNullOrEmpty(txtMailStreetName.Text))
                {

                    Property mail = new Property();
                    mail.StreetNumer = txtMailStreetNumber.Text;
                    mail.StreetName = txtMailStreetName.Text;
                    if (!string.IsNullOrEmpty(ddlState2.Text))
                        mail.StateId = int.Parse(ddlState2.SelectedItem.Value);
                    if (!string.IsNullOrEmpty(ddlCity2.Text))
                        mail.CityId = int.Parse(ddlCity2.SelectedItem.Value);
                    mail.ZipCode = txtZipCode_New.Text;
                    //Add MaillingAddress
                    propertyEntity.Add(mail);
                    if (mail.PropertyId > 0)
                        item.MailingAddressId = mail.PropertyId;
                }

                //Transaction Details
                if (!string.IsNullOrEmpty(txtPurchasePrice.Text))
                {
                    item.PurchaseSoldPrice = decimal.Parse(txtPurchasePrice.Text);
                }

                if (!string.IsNullOrEmpty(txtTotalEquity.Text))
                {
                    item.TotalEquity = decimal.Parse(txtTotalEquity.Text);
                }

                if (rdpPurchaseDate.SelectedDate != null)
                    item.ContractDate = rdpPurchaseDate.SelectedDate;

                if (!string.IsNullOrEmpty(txtLoanAmount.Text))
                {
                    item.LoanAmount = int.Parse(txtLoanAmount.Text);
                }

                if (rdpDateOfCOE.SelectedDate != null)
                    item.DateOfCOE = rdpDateOfCOE.SelectedDate;

                item.Program = txtProgram.Text;

                //Financial Info
                if (!string.IsNullOrEmpty(txtHouseHoldIncome.Text))
                    item.HouseHoldGrossIncome = decimal.Parse(txtHouseHoldIncome.Text);
                if (!string.IsNullOrEmpty(txtHouseHoldDebits.Text))
                    item.HouseHoldDebits = decimal.Parse(txtHouseHoldDebits.Text);

                //TransactionStatus_RE
                if (!string.IsNullOrEmpty(ddlCaseStatus.Text))
                    item.StatusId = int.Parse(ddlCaseStatus.SelectedItem.Value);
                item.DateOfStatus = DateTime.Now;

                //My Income Info
                if (!string.IsNullOrEmpty(txtHouseHoldDebits.Text))
                    item.TotalGrossIncome = decimal.Parse(txtHouseHoldDebits.Text);
                if (!string.IsNullOrEmpty(txtCreditToClient.Text))
                    item.CreditToClient = decimal.Parse(txtCreditToClient.Text);
                item.WayToCredit = txtWayToCredit.Text;

                if (!string.IsNullOrEmpty(txtExpense.Text))
                    item.Expense = decimal.Parse(txtExpense.Text);

                item.ExpenseType = txtExpenseType.Text;
                if (!string.IsNullOrEmpty(txtPayToAssistant.Text))
                    item.AssistantFee = decimal.Parse(txtPayToAssistant.Text);
                item.AssisistantName = txtAssistantName.Text;
                if (!string.IsNullOrEmpty(txtReferalFee.Text))
                    item.ReferalFee = decimal.Parse(txtReferalFee.Text);
                if (!string.IsNullOrEmpty(txtGrossCommission.Text))
                    item.GrossCommission = decimal.Parse(txtGrossCommission.Text);
                if (!string.IsNullOrEmpty(txtMyGrossIncome.Text))
                    item.MyGrossIncome = decimal.Parse(txtMyGrossIncome.Text);
                if (!string.IsNullOrEmpty(txtOtherIncome.Text))
                    item.OtherIncome = decimal.Parse(txtOtherIncome.Text);
                if (!string.IsNullOrEmpty(txtTransactionFee.Text))
                    item.TransactionFee = decimal.Parse(txtTransactionFee.Text);

                item.ReasonForOtherIncome = txtReasonForOtherIncome.Text;
                //Notes
                item.Notes = txtNotes.Text;
                entity.Add(item);

                //Add Status History                
                TransactionStatus status = new TransactionStatus();
                if (!string.IsNullOrEmpty(ddlCaseStatus.Text))
                {
                    status.StatusId = int.Parse(ddlCaseStatus.SelectedItem.Value);
                    status.Date = DateTime.Now;
                    status.TransactionId = item.TransactionId;
                    statusEntity.Add(status);
                }
                //Back to list
                BackUrl();
            }
            catch (Exception ex)
            { }
        }

        protected void imgBack_Click(object sender, ImageClickEventArgs e)
        {
            BackUrl();
        }

        private void BackUrl()
        {
            Response.Redirect("ByREStatus.aspx");
        }

        protected void ddlClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlClient.Text != string.Empty)
            {
                LoadClient(int.Parse(ddlClient.SelectedItem.Value));
            }
            else
            {
                LoadClient(0);
            }
        }

        protected void ddlCoClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCoClient.Text != string.Empty)
            {
                LoadCoClient(int.Parse(ddlCoClient.SelectedItem.Value));
            }
            else
            {
                LoadCoClient(0);
            }
        }

        private void LoadClient(int id)
        {
            ClientEntities entity = new ClientEntities();
            Client client = entity.GetById(id);
            if (client != null)
            {
                txtClientCellPhone.Text = client.CellNumber;
                txtClientEmail.Text = client.Email;
                txtClientFaxNumber.Text = client.FaxNumber;
                txtClientFirstName.Text = client.FirstName;
                txtClientHomePhone.Text = client.HomePhNumber;
                txtClientLastName.Text = client.LastName;
                txtClientSSN.Text = client.SSN;
                txtClientWorkPhone.Text = client.WorkPhNumber;
                if (client.BirthDate != null)
                    rdpClientBirthDate.SelectedDate = client.BirthDate;
            }
        }

        private void LoadCoClient(int id)
        {
            ClientEntities entity = new ClientEntities();
            Client client = entity.GetById(id);
            if (client != null)
            {
                txtCoClientCellPhone.Text = client.CellNumber;
                txtCoClientEmail.Text = client.Email;
                txtCoClientFaxNumber.Text = client.FaxNumber;
                txtCoClientFirstName.Text = client.FirstName;
                txtCoClientHomePhone.Text = client.HomePhNumber;
                txtCoClientLastName.Text = client.LastName;
                txtCoClientSSN.Text = client.SSN;
                txtCoClientWorkPhone.Text = client.WorkPhNumber;
                if (client.BirthDate != null)
                    rdpCoClientBirthDate.SelectedDate = client.BirthDate;
            }
        }

        protected void ddlSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSubject.Text != string.Empty)
            {
                LoadSubject(int.Parse(ddlSubject.SelectedItem.Value));
            }
            else
            {
                LoadSubject(0);
            }
        }

        protected void ddlMailing_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlMailing.Text != string.Empty)
            {
                LoadMailing(int.Parse(ddlMailing.SelectedItem.Value));
            }
            else
            {
                LoadMailing(0);
            }
        }

        private void LoadSubject(int id)
        {
            PropertyEntities entity = new PropertyEntities();
            Property subject = entity.GetById(id);
            if (subject != null)
            {
                ddlSubjectPropertyType.SelectedIndex = Utilities.GetIndexByValue(ddlSubjectPropertyType, subject.PropertyTypeId.ToString());
                txtSubjectStreetNumber.Text = subject.StreetNumer;
                txtSubjectStreetName.Text = subject.StreetName;
                ddlCity1.SelectedIndex = Utilities.GetIndexByValue(ddlCity1, subject.CityId.ToString());
                txtZipCode.Text = subject.ZipCode;
            }
            else
            {
                txtSubjectStreetNumber.Text = string.Empty;
                txtSubjectStreetName.Text = string.Empty;
                ddlCity1.SelectedIndex = 0;
                txtZipCode.Text = string.Empty;
            }
        }

        private void LoadMailing(int id)
        {
            PropertyEntities entity = new PropertyEntities();
            Property mail = entity.GetById(id);
            if (mail != null)
            {
                txtMailStreetNumber.Text = mail.StreetNumer;
                txtMailStreetName.Text = mail.StreetName;
                ddlCity2.SelectedIndex = Utilities.GetIndexByValue(ddlCity2, mail.CityId.ToString());
                txtZipCode_New.Text = mail.ZipCode;
            }
            else
            {
                txtMailStreetNumber.Text = string.Empty;
                txtMailStreetName.Text = string.Empty;
                ddlCity2.SelectedIndex = 0;
                txtZipCode_New.Text = string.Empty;
            }
        }

        protected void ddlState1_SelectedIndexChanged(object sender, EventArgs e)
        {
            StateEntities entity = new StateEntities();
            if (ddlState1.Text != string.Empty)
            {
                LoadCity1();
            }
            else
                Utilities.LoadCityForDropDownlist(ddlCity1);

        }

        protected void ddlState2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlState2.Text != string.Empty)
            {
                LoadCity2();
            }
            else
                Utilities.LoadCityForDropDownlist(ddlCity2);
        }

        private void LoadCity1()
        {
            StateEntities entity = new StateEntities();
            ddlCity1.DataSource = entity.GetCityById(int.Parse(ddlState1.SelectedItem.Value));
            ddlCity1.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddlCity1.Items.Insert(0, item);
        }

        private void LoadCity2()
        {
            StateEntities entity = new StateEntities();
            ddlCity2.DataSource = entity.GetCityById(int.Parse(ddlState2.SelectedItem.Value));
            ddlCity2.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddlCity2.Items.Insert(0, item);
        }

        protected void ddlCity1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CityEntities entity = new CityEntities();
            if (ddlCity1.Text != string.Empty)
                ddlState1.SelectedIndex = Utilities.GetIndexByValue(ddlState1, entity.GetById(int.Parse(ddlCity1.SelectedItem.Value)).StateId.ToString());
        }

        protected void ddlCity2_SelectedIndexChanged(object sender, EventArgs e)
        {
            CityEntities entity = new CityEntities();
            if (ddlCity2.Text != string.Empty)
                ddlState2.SelectedIndex = Utilities.GetIndexByValue(ddlState2, entity.GetById(int.Parse(ddlCity2.SelectedItem.Value)).StateId.ToString());
        }

        protected void chkSame_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSame.Checked)
            {
                if (!string.IsNullOrEmpty(ddlSubject.Text))
                    ddlMailing.SelectedIndex = Utilities.GetIndexByValue(ddlSubject, ddlSubject.SelectedItem.Value);
                if (!string.IsNullOrEmpty(txtSubjectStreetName.Text))
                    txtMailStreetName.Text = txtSubjectStreetName.Text;
                if (!string.IsNullOrEmpty(txtSubjectStreetNumber.Text))
                    txtMailStreetNumber.Text = txtSubjectStreetNumber.Text;
                if (!string.IsNullOrEmpty(ddlState1.Text))
                {
                    ddlState2.SelectedIndex = Utilities.GetIndexByValue(ddlState1, ddlState1.SelectedItem.Value);
                    LoadCity2();
                }

                if (!string.IsNullOrEmpty(ddlCity1.Text))
                    ddlCity2.SelectedIndex = Utilities.GetIndexByValue(ddlCity1, ddlCity1.SelectedItem.Value);
                if (!string.IsNullOrEmpty(txtZipCode.Text))
                    txtZipCode_New.Text = txtZipCode.Text;
            }
        }
    }
}
